﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core;
using System.Text.RegularExpressions;
using COFE.Core.Utils;

namespace COFE.Core.Logger
{
    public class FileSystemEventDispatcher : 
        EventDispatcher<FileSystemEventArgsExA>
    {

        #region Constructor

        #endregion

        #region Static Methods

        protected override bool matchListener(IEventListener<FileSystemEventArgsExA> listener, FileSystemEventArgsExA args)
        {
            Func<string[], bool> isPatternMatched = (patterns) =>
            {
                if (patterns == null || patterns.Length == 0)
                    return true;

                if (RegexUtils.MatchAny(args.ParsePath, patterns))
                    return true;
                if (args is RenameEventArgsExA)
                    if (RegexUtils.MatchAny((args as RenameEventArgsExA).OldParsePath, patterns))
                        return true;
                return false;
            };
            Func<string[], bool> isListerMatched = (keys) =>
            {
                if (keys == null || keys.Length == 0)
                    return true;

                foreach (var key in keys)
                    if (key == args.ListerKey)
                        return true;

                return false;
            };

            var fsListener = listener as IFileSystemListener;
            if (isListerMatched(fsListener.ListenListers))
                if (isPatternMatched(fsListener.ListenPatterns))
                    return true;

            return false;
        }


        #endregion

        #region Methods

        #endregion

        #region Data

        #endregion

        #region Public Properties

        #endregion
    }
}
